package com.cloudcc.mobile.manager;

import android.app.Service;
import android.os.Vibrator;
import android.text.TextUtils;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.cloudcc.cloudframe.util.LogUtils;
import com.cloudcc.cloudframe.util.StringUtils;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.manager.local.LocationService;
import com.cloudcc.mobile.manager.local.WriteLog;

/**
 * @Description: 定位管理器
 * @author wangzhiqiang
 * @date 2015-11-9 上午10:25:35
 * 
 */
public class LocationManager {

	private static LocationManager instance;
	private AppContext context;

	private LocationManager(AppContext context) {
		this.context = context;
	}

	public synchronized static LocationManager getInstance() {
		if (instance == null) {
			instance = new LocationManager(AppContext.getInstance());
		}
		return instance;
	}

	/**
	 * 初始化百度定位
	 */
	public void init() {
		context.locationService = new LocationService(context);
		context.mVibrator = (Vibrator) context
				.getSystemService(Service.VIBRATOR_SERVICE);
		WriteLog.getInstance().init(); // 初始化日志
	}

	public LocationService getLocationService() {
		if (context.locationService == null) {
			init();
		}
		return context.locationService;
	}

	/**
	 * 请求位置
	 * 
	 * @param listener
	 */
	public void requestLocal() {
		LocationService locationService = getLocationService();
		locationService.setLocationOption(locationService.getOption());
		locationService.start();
	}

	/**
	 * 停止定位服务
	 * 
	 * @param listener
	 */
	public void stopRequestLocal() {
		LocationService locationService = getLocationService();
		locationService.stop(); // 停止定位服务
	}

	/**
	 * 注销监听
	 * 
	 * @param listener
	 */
	public void unregisterlistener(BDLocationListener listener) {
		LocationService locationService = getLocationService();
		locationService.unregisterListener(listener);
	}

	/**
	 * 注册监听
	 * 
	 * @param listener
	 */
	public void registerListener(BDLocationListener listener) {
		LocationService locationService = getLocationService();
		locationService.registerListener(listener);
	}

	/**
	 * 根据 地位地址 获得 详细地址
	 * 
	 * @param location
	 * @param hasN
	 *            是否 在 详情地址之间加上分行
	 * @return
	 */
	public static String getDetailAddr(BDLocation location, boolean hasN) {
		if (location == null) {
			return "";
		}
		if (TextUtils.isEmpty(location.getAddrStr())) {
			return "";
		}
		StringBuilder sb = new StringBuilder();
		sb.append(location.getAddrStr());
		if (hasN) {
			sb.append("\n");
		}
		if (!TextUtils.isEmpty(location.getLocationDescribe())) {
			sb.append(StringUtils.remove(location.getLocationDescribe(), "在"));
		}
		return sb.toString();
	}

	public static void log(BDLocation location) {
		StringBuffer sb = new StringBuffer(256);
		sb.append("time : ");
		/**
		 * 时间也可以使用systemClock.elapsedRealtime()方法 获取的是自从开机以来，每次回调的时间；
		 * location.getTime() 是指服务端出本次结果的时间，如果位置不发生变化，则时间不变
		 */
		sb.append(location.getTime());
		sb.append("\nerror code : ");
		sb.append(location.getLocType());
		sb.append("\nlatitude : ");
		sb.append(location.getLatitude());
		sb.append("\nlontitude : ");
		sb.append(location.getLongitude());
		sb.append("\nradius : ");
		sb.append(location.getRadius());
		sb.append("\nCountryCode : ");
		sb.append(location.getCountryCode());
		sb.append("\nCountry : ");
		sb.append(location.getCountry());
		sb.append("\ncitycode : ");
		sb.append(location.getCityCode());
		sb.append("\ncity : ");
		sb.append(location.getCity());
		sb.append("\nDistrict : ");
		sb.append(location.getDistrict());
		sb.append("\nStreet : ");
		sb.append(location.getStreet());
		sb.append("\naddr : ");
		sb.append(location.getAddrStr());
		sb.append("\nDescribe: ");
		sb.append(location.getLocationDescribe());
		sb.append("\nDirection(not all devices have value): ");
		sb.append(location.getDirection());
		sb.append("\nPoi: ");
		if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果
			sb.append("\nspeed : ");
			sb.append(location.getSpeed());// 单位：km/h
			sb.append("\nsatellite : ");
			sb.append(location.getSatelliteNumber());
			sb.append("\nheight : ");
			sb.append(location.getAltitude());// 单位：米
			sb.append("\ndescribe : ");
			sb.append("gps定位成功");
		} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果
			// 运营商信息
			sb.append("\noperationers : ");
			sb.append(location.getOperators());
			sb.append("\ndescribe : ");
			sb.append("网络定位成功");
		} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
			sb.append("\ndescribe : ");
			sb.append("离线定位成功，离线定位结果也是有效的");
		} else if (location.getLocType() == BDLocation.TypeServerError) {
			sb.append("\ndescribe : ");
			sb.append("服务端网络定位失败，可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com，会有人追查原因");
		} else if (location.getLocType() == BDLocation.TypeNetWorkException) {
			sb.append("\ndescribe : ");
			sb.append("网络不同导致定位失败，请检查网络是否通畅");
		} else if (location.getLocType() == BDLocation.TypeCriteriaException) {
			sb.append("\ndescribe : ");
			sb.append("无法获取有效定位依据导致定位失败，一般是由于手机的原因，处于飞行模式下一般会造成这种结果，可以试着重启手机");
		}
		LogUtils.d("baidumap", sb.toString());
	}

}
